package org.clipjoint.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 	图片验证码控制器
 * @author 陈光耀
 * date: 2021-2-22
 *
 */
@WebServlet("/captcha")
public class CaptchaController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 创建一个带有干扰线的验证码图片（200px*100px）
		LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(110, 40, 4, 150);
		// 获取验证码文本
		String code = lineCaptcha.getCode();
		// 将正确的验证码存入session
		request.getSession().setAttribute("captchaCode", code);
		// 将验证码图片写回到浏览器
		lineCaptcha.write(response.getOutputStream());
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
